
import { Body, Controller, Get, Post, Param, Put, Delete } from "@nestjs/common";
import { DeviceService } from './app.service';
import { DeviceDTO, DeviceMessageDTO } from "./dto/DeviceDTO";
import deviceManager from "./deviceManager";

function success(data:any = null) {
  return {
    success: true,
    data: data
  };
}

@Controller('device')
export class DeviceController {

  constructor(private readonly deviceService: DeviceService) {}

  @Get()
  list() {
    return this.deviceService.list();
  }

  /**
   * 发布设备
   */
  @Post()
  add(@Body() dto: DeviceDTO) {
    return this.deviceService.add(dto);
  }

  /**
   * 向设备发送消息
   */
  @Post('addMessage')
  async addMessage(@Body() dto: DeviceMessageDTO) {
    return this.deviceService.addMessage(dto.deviceId, dto.message);
  }

  /**
   * 关闭设备
   */
  @Put('shutdown/:id')
  shutdown(@Param("id") id: number) {
    deviceManager.shutdown(id);
    return success();
  }

  /**
   * 关闭设备
   */
  @Put('start/:id')
  start(@Param("id") id: number) {
    return deviceManager.start(id);
  }

  /**
   * 删除设备
   */
  @Delete('del/:id')
  async del(@Param("id") id: number) {
    await this.deviceService.del(id)
    return success();
  }

  @Get('getStatus/:id')
  getStatus(@Param("id") id: number) {
    return {
      status: this.deviceService.getStatus(id)
    };
  }

  @Get('getDetail/:id')
  getDetail(@Param("id") id: number) {
    return this.deviceService.getDetail(id);
  }

}
